home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18367 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.6 KB

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help! What's wrong with this?
  5. Date: Fri, 19 Apr 1996 14:32:07 GMT
  6. Organization: Netcom
  7. Message-ID: <3177a34c.144208280@nntp.ix.netcom.com>
  8. References: <4l22v3$1beo@useneta1.news.prodigy.com> <317660A6.56C7@datalytics.com>
  9. NNTP-Posting-Host: ix-dc14-19.ix.netcom.com
  10. X-NETCOM-Date: Fri Apr 19  9:31:49 AM CDT 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Rob Stewart <stew@datalytics.com> wrote:
  14.  
  15. > Brian Munroe wrote:
  16. > > 
  17. > > I'm trying to learn C++ and I've run into a strange problem in the
  18. > > following code.
  19. > > 
  20. > First, the code you submitted is C, not C++.  Just because 
  21. > you're using a C++ compiler on it does not make it C++.  Let's 
  22. > call it what it is.
  23.  
  24. OK.  Let's call it C++.  The program Brian submitted will not compile
  25. on any properly working C compiler.
  26.  
  27. > > int const ADD_LEN = 40;
  28. > Try "const int ADD_LEN = 40" instead.
  29.  
  30. Just because Brian is obviously a beginner, there's no reason to give
  31. him poor advice.  A programmer should not "try" things -- he should
  32. understand the language definition.  One who does will know that "int
  33. const ADD_LEN = 40;" and "const int ADD_LEN = 40;" are equivalent.
  34. There's no reason to change it.
  35.  
  36. > > int main()
  37. > > {
  38. > >   struct cust_list {
  39. > >     char name[ADD_LEN];
  40. > >     char add1[ADD_LEN];
  41. > >     char add2[ADD_LEN];
  42. > >     char add3[ADD_LEN];
  43. > >   } customer;
  44. > >   int flag;
  45. > > 
  46. > >   void get_customer_info(char temp[ADD_LEN], int flag);
  47. > This declaration should really come before main.
  48. > > 
  49. > >   for (flag=0; flag<4; flag++)
  50. > >   { switch (flag)
  51. > >     { case 0: get_customer_info(customer.name, flag); break;
  52. > >       case 1: get_customer_info(customer.add1, flag); break;
  53. > >       case 2: get_customer_info(customer.add2, flag); break;
  54. > >       case 3: get_customer_info(customer.add3, flag); break;
  55. > >       default: exit(1);
  56. > >     }
  57. > >   }
  58. > This is the oddest thing I've ever seen.  Don't play games like 
  59. > this, just write the following:
  60. >     get_customer_info(customer.name, 0);
  61. >     get_customer_info(customer.add1, 1);
  62. >     get_customer_info(customer.add2, 2);
  63. >     get_customer_info(customer.add3, 3);
  64. > Having said that, it would be better for you to supply the 
  65. > prompt string to get_customer_info rather than using a flag to 
  66. > do it.  This allows the caller to more appopriately associate 
  67. > the text of the prompt with the buffer being filled.
  68. > >   return 0;
  69. > > }
  70. > > 
  71. > > void get_customer_info(char temp[ADD_LEN], int flag)
  72. > FYI: Declaring temp as char[ADD_LEN] doesn't guarrantee that you 
  73. > get an array of that size.
  74. > > { int answer;
  75. > >   int len;
  76. > >   clrscr();
  77. > >   switch (flag)
  78. > >   { case 0:
  79. > >      cout << "Enter the bidder's name in one of the following forms.\n";
  80. > >      cout << "  Person: LAST NAME, FIRST NAME\n";
  81. > >      cout << "  Business: COMPANY NAME\n\n";
  82. > >      break;
  83. > >     case 1:
  84. > >      cout << "\nEnter first line of address\n";
  85. > >      break;
  86. > >     case 2:
  87. > >      cout << "\nEnter second line of address\n";
  88. > >      break;
  89. > >     case 3:
  90. > >      cout << "\nEnter third line of address\n";
  91. > >      break;
  92. > >     default:
  93. > >      exit(1);
  94. > >   }
  95. > >   cin >> temp;
  96. > Try this instead.  It will eat whitespace before filling temp.
  97. >     cin >> ws >> temp;
  98.  
  99. As opposed to
  100.  
  101.     cin >> temp;
  102.  
  103. which eats whitespace before filling temp.  The code Brian gave is
  104. correct -- why suggest a change that does nothing.
  105.  
  106. > >   len = strlen(temp);
  107. > >   if (len < ADD_LEN)
  108. > >    { strncat(temp, "                                        ",ADD_LEN-
  109. > > len);}
  110. > >   temp[ADD_LEN-1] = '\0';
  111. > Rather than have get_customer_info be dependent upon a global 
  112. > value, why not pass the size of temp to it.  This reduces 
  113. > dependence on the global to main, which is a better habit to 
  114. > develop (later maintenance of code is easier with restricted 
  115. > dependence on globals).
  116. > >   return;
  117. > > }
  118. > > 
  119. > > When I run this all 4 prompts are outputted (Is that a word?), but only
  120. > > input for the name and second line are  accepted. The input line doesn't
  121. > > accept input for the first and third lines. I'm guessing that there is a
  122. > > return character caught in the input buffer after I input the name and
  123. > > second address lines and it's read in for the first and third lines. If
  124. > > so, how do I get it out of the buffer and what the heck is it doing there
  125. > > in the first place !?
  126.  
  127. Brian's problem is that he is entering a name that contains spaces.
  128. The the first input gets the string from the first non-whitespace
  129. character to the first space and the next input gets the next string
  130. from what's already been entered.
  131.  
  132.  
  133. Michael M Rubenstein
  134.